import { defineStore } from "pinia";
import { 
    getStudentApi, 
    getGradeInfoApi, 
    getRoomNameApi, 
    getBedNumberApi,
    searchStudentApi
} from '@/service/student'
import { getBuildingsDataApi } from '@/service/buildings'
export const useStudentStore = defineStore('studentStore', {
    state() {
        return {
            isShowStudentPanel: false, 
            isDialogVisible: false,
            isRetreat: false,   // 是否显示退宿面板
            panelTitle: '', // 面板标题
            openPanelType: '', // 打开面板类型
            tableData: [],
            buildings: [],
            grades: [],
            room_names: [],
            bed_numbers: [],
            notDormitoryLeader: [],
            dormitoryNumbers: [],
            dorId: '',   
            bedId: '',
            sno: '',    // 需要操作的学生信息的学号
        }
    },
    actions: {
        // 获取所有的学生信息
        async getStudentData(type = 'all') {
            this.tableData = [];
            const res = await getStudentApi(type);
            const { data } = res.data;
            this.tableData = data;
        },
        // 获取年级
        async getGradeInfo() {
            const res = await getGradeInfoApi();
            const { data } = res.data;
            this.grades = data;
        },
        // 获取可用的宿舍楼
        async getBuildingsData(type) {
            this.buildings = [];
            const res = await getBuildingsDataApi(type, 1);
            const { data } = res.data;
            this.buildings = data;
        },
        // 获取还有空余床位的宿舍号
        async getRoomName(building) {
            const res = await getRoomNameApi(building);
            const { data } = res.data;
            this.room_names = data;
        },
        // 根据宿舍号获取所有的空余床位
        async getBedNumber(id) {
            const res = await getBedNumberApi(id);
            const { data } = res.data;
            this.bed_numbers = data;
        },
        // 搜索学生信息
        async searchStudentInfo(key, value) {
            const res = await searchStudentApi(key, value);
            this.tableData = [];
            this.tableData = res.data.data;
        }
    }
})  